<template>
  <el-card class="statistic-card" :class="className" @click="handleClick">
    <div class="card-content">
      <div class="card-icon" :style="{ backgroundColor: iconBgColor }">
        <el-icon :size="32"><component :is="icon" /></el-icon>
      </div>
      <div class="card-info">
        <div class="card-title">{{ title }}</div>
        <div class="card-value" :style="{ color: valueColor }">{{ formattedValue }}</div>
        <div v-if="subTitle" class="card-subtitle">
          <span>{{ subTitle }}</span>
          <span v-if="trend" :class="['trend', trend === 'up' ? 'trend-up' : 'trend-down']">
            <el-icon v-if="trend === 'up'"><ArrowUp /></el-icon>
            <el-icon v-else><ArrowDown /></el-icon>
            {{ trendValue }}
          </span>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, PropType } from 'vue'
import { ArrowUp, ArrowDown } from '@element-plus/icons-vue'

const props = defineProps({
  title: {
    type: String,
    required: true
  },
  value: {
    type: Number,
    required: true
  },
  icon: {
    type: String,
    required: true
  },
  iconBgColor: {
    type: String,
    default: '#409EFF'
  },
  valueColor: {
    type: String,
    default: '#409EFF'
  },
  className: {
    type: String,
    default: ''
  },
  subTitle: {
    type: String,
    default: ''
  },
  trend: {
    type: String as PropType<'up' | 'down' | ''>,
    default: ''
  },
  trendValue: {
    type: String,
    default: ''
  },
  duration: {
    type: Number,
    default: 2000
  }
})

const emit = defineEmits(['click'])

const currentValue = ref(0)

// 数字格式化（添加千位分隔符）
const formatNumber = (num: number): string => {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

const formattedValue = computed(() => {
  return formatNumber(currentValue.value)
})

// 处理卡片点击
const handleClick = () => {
  emit('click')
}

// 数值动画函数
const animateValue = (start: number, end: number, duration: number) => {
  let startTimestamp: number | null = null
  const step = (timestamp: number) => {
    if (!startTimestamp) startTimestamp = timestamp
    const progress = Math.min((timestamp - startTimestamp) / duration, 1)
    currentValue.value = Math.floor(progress * (end - start) + start)
    
    if (progress < 1) {
      window.requestAnimationFrame(step)
    }
  }
  window.requestAnimationFrame(step)
}

onMounted(() => {
  // 开始数值动画
  animateValue(0, props.value, props.duration)
})
</script>

<style scoped>
.statistic-card {
  transition: all 0.3s;
  cursor: pointer;
}

.statistic-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.card-content {
  display: flex;
  align-items: center;
}

.card-icon {
  width: 64px;
  height: 64px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-right: 16px;
}

.card-info {
  flex: 1;
}

.card-title {
  font-size: 14px;
  color: var(--text-secondary, #909399);
}

.card-value {
  font-size: 24px;
  font-weight: bold;
  margin: 8px 0;
  transition: color 0.3s ease;
}

.card-subtitle {
  font-size: 12px;
  color: var(--text-secondary, #909399);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.trend {
  display: flex;
  align-items: center;
  font-weight: 500;
}

.trend-up {
  color: #67C23A;
}

.trend-down {
  color: #F56C6C;
}

.trend .el-icon {
  margin-right: 4px;
}
</style> 